﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PizzaSystem
{
    public partial class AdminApp : Form
    {
        public AdminApp()
        {
            InitializeComponent();
        }
        public AdminApp(AppHome app, Admin admin)
        {
            InitializeComponent();
            this.app = app;
            this.admin = admin;
            dataGridView1.RowHeadersWidthSizeMode = 0;
            getPizza();
        }

        AppHome app;

        Admin admin;

        SqlDao sqlDao = new SqlDao();

        bool e2 = true, e3 = true, e4 = true;
        //界面切换
        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPageIndex==0)
            {
                getPizza();
            }
            else if (e.TabPageIndex==1)
            {
                //订单信息
                DataSet ds = sqlDao.selectBill(0);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView2.DataSource = ds.Tables[0].DefaultView;
                    if (e2)
                    {
                        DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                        column.Text = "交 付";
                        column.Name = "编 辑";
                        column.UseColumnTextForButtonValue = true;
                        dataGridView2.Columns.Add(column);
                        e2 = false;
                    }
                }
            }
            else if(e.TabPageIndex==2)
            {
                //原料
                DataSet ds = sqlDao.selectInventory();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView3.DataSource = ds.Tables[0].DefaultView;
                    if (e3)
                    {
                        DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                        column.Text = "采 购";
                        column.Name = "编 辑";
                        column.UseColumnTextForButtonValue = true;
                        dataGridView3.Columns.Add(column);
                        e3 = false;
                    }
                }
            }
            else if (e.TabPageIndex == 3){
                //生产计划
                DataSet ds = sqlDao.selectScheme();
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView4.DataSource = ds.Tables[0].DefaultView;
                    if (e4)
                    {
                        DataGridViewButtonColumn column = new DataGridViewButtonColumn();
                        column.Text = "生 产";
                        column.Name = "编 辑";
                        column.UseColumnTextForButtonValue = true;
                        dataGridView4.Columns.Add(column);
                        e4 = false;
                    }
                }
            }
            else if(e.TabPageIndex == 4){
                if (MessageBox.Show("确定要退出？", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    app.openForm(new Home(app));
            }
        }

        //交付
        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 &&
                MessageBox.Show("确定交付披萨？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Bill bill = new Bill();
                bill.B_id = dataGridView2.Rows[e.RowIndex].Cells["订单号"].Value.ToString();
                bill.C_id = long.Parse(dataGridView2.Rows[e.RowIndex].Cells["客户ID"].Value.ToString());
                bill.P_id = long.Parse(dataGridView2.Rows[e.RowIndex].Cells["披萨编号"].Value.ToString());
                bill.B_num = int.Parse(dataGridView2.Rows[e.RowIndex].Cells["数量"].Value.ToString());
                bill.B_money = decimal.Parse(dataGridView2.Rows[e.RowIndex].Cells["金额"].Value.ToString());
                bill.B_real_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                bill.B_state = 1;
                if (sqlDao.updateBillFinish(bill))
                {
                    MessageBox.Show("交付完成...");
                    dataGridView2.Rows.RemoveAt(e.RowIndex);
                }
                else
                {
                    MessageBox.Show("库存不足...");
                }
            }
        }

        //采购
        private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0)
            {
                Inventory inventory = new Inventory();
                inventory.I_price = decimal.Parse(dataGridView3.Rows[e.RowIndex].Cells["价格"].Value.ToString());
                inventory.I_id = long.Parse(dataGridView3.Rows[e.RowIndex].Cells["材料编号"].Value.ToString());
                inventory.V_id = long.Parse(dataGridView3.Rows[e.RowIndex].Cells["供应商号"].Value.ToString());
                inventory.I_name = dataGridView3.Rows[e.RowIndex].Cells["材料名"].Value.ToString();
                BuyInventory buyInventory = new BuyInventory(inventory);
                buyInventory.ShowDialog();
            }
        }

        //制定计划
        private void create_Click(object sender, EventArgs e)
        {
            CreatePlan createPlan = new CreatePlan();
            createPlan.ShowDialog();
        }

        //生产
        private void dataGridView4_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex == 0 &&
                MessageBox.Show("确定要先生产该披萨？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                long p_id = long.Parse(dataGridView4.Rows[e.RowIndex].Cells["披萨编号"].Value.ToString());
                int num = int.Parse(dataGridView4.Rows[e.RowIndex].Cells["生产数量"].Value.ToString());
                if (sqlDao.updateInventoryNum(p_id, num))
                {
                    MessageBox.Show("生产成功...");
                    dataGridView4.Rows.RemoveAt(e.RowIndex);
                }
                else
                {
                    MessageBox.Show("生产失败，原材料库存不足....");
                }
            }
        }

        private void getPizza()
        {
            DataSet ds = sqlDao.selectPizzaToDs();
            if (ds.Tables[0].Rows.Count > 0)
            {
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
        }
    }
}
